package tbs.littlebaby;

import Data.BasicData;
import Data.ErrorLoger;
import UI.ExpandableListADP;
import UI.ImageRequestActivity;
import UI.SerachADP;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.MultiAutoCompleteTextView;
import android.widget.Toast;

public class ItemList extends ImageRequestActivity {
    public  static void refreashList(int group)
    {
        list.collapseGroup(group);
        list.expandGroup(group);
    }
    ExpandableListADP adp;
    BasicData[] ReadData(int pos)
    {
       return MainActivity.save.Read(pos);
    }
    SerachADP SerachData(BasicData[] datas)
    {
        SerachADP adapter=new SerachADP(this,datas);
        return adapter;
    }
    public static ExpandableListView list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_list);
        MultiAutoCompleteTextView search=findViewById(R.id.search);
        try {
            int pos= this.getIntent().getIntExtra("pos",-1);
            list=findViewById(R.id.list);
            String[] arr= this.getIntent().getStringArrayExtra("parts");
            BasicData[] data=ReadData(pos);
            adp=new ExpandableListADP(this,pos,arr, data);
            SerachADP serachADP=SerachData(data);
            search.setAdapter(serachADP);
            search.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    BasicData d=(BasicData) serachADP.getItem(position);
                    list.expandGroup((int) id,true);
                    list.setSelectedChild(d.getType(),(int)id,true);

                }
            });
            search.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
            list.setAdapter(adp);
        }catch (Exception ex)
        {
            ErrorLoger.Log(ex);
            Toast.makeText(this,ex.getMessage()+"   "+ex.getStackTrace(),Toast.LENGTH_LONG).show();
        }


    }

    @Override
    public void Handle(Message msg) {
        if(msg.what==1)
        {
                adp.childs[adp.nowGroup].get(adp.nowChild).setImage((Bitmap)msg.obj);
                MainActivity.save.Alter(adp.childs[adp.nowGroup].get(adp.nowChild).getID(),adp.childs[adp.nowGroup].get(adp.nowChild));
                refreashList(adp.nowGroup);
        }
        else
        {
            Toast.makeText(ItemList.this,"读取错误图片",Toast.LENGTH_SHORT).show();
        }
    }
}